package com.wangqinlei.cowims126.controller;

import com.wangqinlei.cowims126.pojo.Cow;
import com.wangqinlei.cowims126.service.CategoryService;
import com.wangqinlei.cowims126.service.CowService;
import com.wangqinlei.cowims126.service.FeedService;
import com.wangqinlei.cowims126.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author 王勤磊
 * CowController控制类
 */
@Controller
@RequestMapping("/cow")
public class CowController {

    @Autowired
    private CowService cowService;
    @Autowired
    private FeedService feedService;
    /**
     * 跳转牛列表页面
     * @return
     */
    @GetMapping("/cow_list")
    public String cowList(){
        return "/cow/cowList";
    }

    /**
     * 异步加载牛列表
     * @param page
     * @param rows
     * @param categoryid
     * @param from
     * @return
     */
    @RequestMapping("/getCowList")
    @ResponseBody
    public Object getCowList(@RequestParam(value = "page", defaultValue = "1")Integer page,
                             @RequestParam(value = "rows", defaultValue = "100")Integer rows,
                             @RequestParam(value = "cowsn", defaultValue = "")String cowSn,
                             @RequestParam(value = "categoryid", defaultValue = "0")String categoryid, String from){
        Map<String,Object> paramMap = new HashMap();
        paramMap.put("pageno",page);
        paramMap.put("pagesize",rows);
        if(!StringUtils.isEmpty(cowSn))  paramMap.put("cowsn",cowSn);
        if(!categoryid.equals("0"))  paramMap.put("categoryid",categoryid);
        PageBean<Cow> pageBean = cowService.queryPage(paramMap);
        if(!StringUtils.isEmpty(from) && from.equals("combox")){
            return pageBean.getDatas();
        }else{
            Map<String,Object> result = new HashMap();
            result.put("total",pageBean.getTotalsize());
            result.put("rows",pageBean.getDatas());
            return result;
        }
    }

    /**
     * 删除牛
     * @param data
     * @return
     */
    @PostMapping("/deleteCow")
    @ResponseBody
    public AjaxResult deleteCow(Data data){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            List<Integer> ids = data.getIds();
            Iterator<Integer> iterator = ids.iterator();
            while (iterator.hasNext()){  //判断是否存在饲养记录关联牛
                if(!feedService.isCowId(iterator.next())){
                    ajaxResult.setSuccess(false);
                    ajaxResult.setMessage("无法删除,存在饲养记录关联牛");
                    return ajaxResult;
                }
            }
            for(Integer id : ids){
                Cow byId = cowService.findById(id);
            }
            int count = cowService.deleteCow(ids);
            if(count > 0){
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("全部删除成功");

            }else{
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("删除失败");
            }

        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("删除失败");
        }
        return ajaxResult;
    }


    /**
     * 添加牛
     * @param cow
     * @return
     * @throws IOException
     */
    @RequestMapping("/addCow")
    @ResponseBody
    public AjaxResult addCow(Cow cow) throws IOException {

        AjaxResult ajaxResult = new AjaxResult();
        cow.setSn(SnGenerateUtil.generateSn(cow.getCategoryId()));
        //保存牛信息到数据库
        try{
            int count = cowService.addCow(cow);
            if(count > 0){
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("保存成功");
            }else{
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("保存失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("保存失败");
        }

        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    /**
     * 修改牛信息
     * @param cow
     * @return
     */
    @PostMapping("/editCow")
    @ResponseBody
    public AjaxResult editCow(Cow cow){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            int count = cowService.editCow(cow);
            if(count > 0){
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("修改成功");
            }else{
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("修改失败");
            }
        }catch(Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("修改失败");
        }
        return ajaxResult;
    }
}
